package com.powernode.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.powernode.config.AlipayConfigConstant;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
public class AlipayTestController {

    @Autowired
    HttpServletResponse response;

    /**
     * 电脑网站支付
     * AlipayClient：支付宝客户端配置
     * AlipayTradePagePayRequest：支付请求
     * AlipayTradePagePayModel：支付参数
     * AlipayTradePagePayResponse：支付响应
     */
    @SneakyThrows
    @GetMapping("/pc")
    public void toPayPage() {
        // 支付请求的客户端对象
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfigConstant.SERVER_URL,
                AlipayConfigConstant.APPID,
                AlipayConfigConstant.PRIVATE_KEY,
                AlipayConfigConstant.FORMAT,
                AlipayConfigConstant.CHARSET,
                AlipayConfigConstant.ALIPAY_PUBLIC_KEY,
                AlipayConfigConstant.SIGN_TYPE);

        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        // 支付参数
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        // 业务系统的订单编号
        model.setOutTradeNo("20190320010101999");
        // 订单金额
        model.setTotalAmount("20");
        // 订单主题
        model.setSubject("Iphone6 16G-11");
        // 产品码
        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        request.setBizModel(model);
        AlipayTradePagePayResponse res = alipayClient.pageExecute(request);
        // System.out.println(res.getBody());
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(res.getBody());
    }

    /**
     * 手机网页支付
     * AlipayClient：支付宝客户端配置
     * AlipayTradeWapPayRequest：支付请求
     * AlipayTradeWapPayModel：支付参数
     * AlipayTradeWapPayResponse：支付响应
     */
    @SneakyThrows
    @GetMapping("/wap")
    public void toPayWap(){
        // 支付请求的客户端对象
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfigConstant.SERVER_URL,
                AlipayConfigConstant.APPID,
                AlipayConfigConstant.PRIVATE_KEY,
                AlipayConfigConstant.FORMAT,
                AlipayConfigConstant.CHARSET,
                AlipayConfigConstant.ALIPAY_PUBLIC_KEY,
                AlipayConfigConstant.SIGN_TYPE);

        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        // 支付参数
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        // 业务系统的订单编号
        model.setOutTradeNo("20190320010101669");
        // 订单金额
        model.setTotalAmount("20");
        // 订单主题
        model.setSubject("Iphone6 16G-11");
        // 产品类型
        model.setProductCode("QUICK_WAP_WAY");
        // 设置支付请求携带的请求参数
        request.setBizModel(model);
        AlipayTradeWapPayResponse res = alipayClient.pageExecute(request);
        // System.out.println(res.getBody());
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(res.getBody());
    }
}
